-----------------------moodle18/course/format/super/format.php (now based on topic format)-----------
libdir.'/ajax/ajaxlib.php');
$topic = optional_param('topic', -1, PARAM_INT);
// Bounds for block widths
// more flexible for theme designers taken from theme config.php
$lmin = (empty($THEME->block_l_min_width)) ? 100 : $THEME->block_l_min_width;
$lmax = (empty($THEME->block_l_max_width)) ? 210 : $THEME->block_l_max_width;
$rmin = (empty($THEME->block_r_min_width)) ? 100 : $THEME->block_r_min_width;
$rmax = (empty($THEME->block_r_max_width)) ? 210 : $THEME->block_r_max_width;
define('BLOCK_L_MIN_WIDTH', $lmin);
define('BLOCK_L_MAX_WIDTH', $lmax);
define('BLOCK_R_MIN_WIDTH', $rmin);
define('BLOCK_R_MAX_WIDTH', $rmax);
$preferred_width_left = bounded_number(BLOCK_L_MIN_WIDTH, blocks_preferred_width($pageblocks[BLOCK_POS_LEFT]),
BLOCK_L_MAX_WIDTH);
$preferred_width_right = bounded_number(BLOCK_R_MIN_WIDTH, blocks_preferred_width($pageblocks[BLOCK_POS_RIGHT]),
BLOCK_R_MAX_WIDTH);
if ($topic != -1) {
$displaysection = course_set_display($course->id, $topic);
} else {
if (isset($USER->display[$course->id])) { // for admins, mostly
$displaysection = $USER->display[$course->id];
} else {
$displaysection = course_set_display($course->id, 0);
}
}
$context = get_context_instance(CONTEXT_COURSE, $course->id);
if (($marker >=0) && has_capability('moodle/course:setcurrentsection', $context) && confirm_sesskey()) {
$course->marker = $marker;
if (! set_field("course", "marker", $marker, "id", $course->id)) {
error("Could not mark that topic for this course");
}
}
$streditsummary = get_string('editsummary');
$stradd = get_string('add');
$stractivities = get_string('activities');
$strshowalltopics = get_string('showalltopics');
$strtopic = get_string('topic');
$strgroups = get_string('groups');
$strgroupmy = get_string('groupmy');
$editing = $PAGE->user_is_editing();
if ($editing) {
$strstudents = moodle_strtolower($course->students);
$strtopichide = get_string('topichide', '', $strstudents);
$strtopicshow = get_string('topicshow', '', $strstudents);
$strmarkthistopic = get_string('markthistopic');
$strmarkedthistopic = get_string('markedthistopic');
$strmoveup = get_string('moveup');
$strmovedown = get_string('movedown');
}
/// Layout the whole page as three big columns.
echo '
';
/// The left column ...
$lt = (empty($THEME->layouttable)) ? array('left', 'middle', 'right') : $THEME->layouttable;
foreach ($lt as $column) {
switch ($column) {
case 'left':
if (blocks_have_content($pageblocks, BLOCK_POS_LEFT) || $editing) {
echo '';
if (!empty($THEME->roundcorners)) {
echo '';
echo '';
}
blocks_print_group($PAGE, $pageblocks, BLOCK_POS_LEFT);
if (!empty($THEME->roundcorners)) {
echo ' ';
echo '';
}
echo ' | ';
}
break;
case 'middle':
/// Start main column
echo '';
if (!empty($THEME->roundcorners)) {
echo '';
echo '';
}
echo ' ';
//print_heading_block(get_string('topicoutline'), 'outline');
print_simple_box_start("center", "$width", '', 5, "generalbox");
echo ' ';
echo ''.$course->fullname.' | ';
echo ''.$course->summary.' | ';
echo ' ';
print_simple_box_end();
if ($subcourses = get_records('course_meta','child_course',$course->id)) {
echo ' ';
foreach ($subcourses as $subcourse) {
echo '';
$subcoursedata = get_record('course','id',$subcourse->parent_course)
;
print_subcourse($subcoursedata, "100%", $course->id);
echo ' | ';
}
echo ' ';
}
echo ' ';
/// If currently moving a file then show the current clipboard
if (ismoving($course->id)) {
$stractivityclipboard = strip_tags(get_string('activityclipboard', '', addslashes($USER->activitycopyname)));
$strcancel= get_string('cancel');
echo '';
echo '';
echo $stractivityclipboard.' ('.$strcancel.')';
echo ' | ';
echo ' ';
}
/// Print Section 0
$section = 0;
$thissection = $sections[$section];
if ($thissection->summary or $thissection->sequence or isediting($course->id)) {
echo '';
echo ' | ';
echo '';
echo '';
$summaryformatoptions->noclean = true;
echo format_text($thissection->summary, FORMAT_HTML, $summaryformatoptions);
if (isediting($course->id)) {
echo '  ';
}
echo ' ';
print_section($course, $thissection, $mods, $modnamesused);
if (isediting($course->id)) {
print_section_add_menus($course, $section, $modnames);
}
echo ' | ';
echo ' | ';
echo ' ';
echo ' | ';
}
/// Now all the normal modules by topic
/// Everything below uses "section" terminology - each "section" is a topic.
$timenow = time();
$section = 1;
$sectionmenu = array();
while ($section <= $course->numsections) {
if (!empty($sections[$section])) {
$thissection = $sections[$section];
} else {
unset($thissection);
$thissection->course = $course->id; // Create a new section structure
$thissection->section = $section;
$thissection->summary = '';
$thissection->visible = 1;
if (!$thissection->id = insert_record('course_sections', $thissection)) {
notify('Error inserting new topic!');
}
}
$showsection = (has_capability('moodle/course:viewhiddensections', $context) or $thissection->visible or !$course->hiddensections);
if (!empty($displaysection) and $displaysection != $section) {
if ($showsection) {
$strsummary = strip_tags(format_string($thissection->summary,true));
if (strlen($strsummary) < 57) {
$strsummary = ' - '.$strsummary;
} else {
$strsummary = ' - '.substr($strsummary, 0, 60).'...';
}
$sectionmenu['topic='.$section] = s($section.$strsummary);
}
$section++;
continue;
}
if ($showsection) {
$currenttopic = ($course->marker == $section);
if (!$thissection->visible) {
$sectionstyle = ' hidden';
} else if ($currenttopic) {
$sectionstyle = ' current';
} else {
$sectionstyle = '';
}
echo '';
echo ''.$section.' | ';
echo '';
if (!has_capability('moodle/course:viewhiddensections', $context) and !$thissection->visible) { // Hidden for students
echo get_string('notavailable');
} else {
echo '';
$summaryformatoptions->noclean = true;
echo format_text($thissection->summary, FORMAT_HTML, $summaryformatoptions);
if (isediting($course->id)) {
echo ' '.
' ';
}
echo ' ';
print_section($course, $thissection, $mods, $modnamesused);
if (isediting($course->id)) {
print_section_add_menus($course, $section, $modnames);
}
}
echo ' | ';
echo '';
if ($displaysection == $section) { // Show the zoom boxes
echo ''.
' ';
} else {
$strshowonlytopic = get_string('showonlytopic', '', $section);
echo ''.
' ';
}
if (isediting($course->id)) {
if ($course->marker == $section) { // Show the "light globe" on/off
echo ''.
' ';
} else {
echo ''.
' ';
}
if ($thissection->visible) { // Show the hide/show eye
echo ''.
' ';
} else {
echo ''.
' ';
}
if ($section > 1) { // Add a arrow to move section up
echo ''.
' ';
}
if ($section < $course->numsections) { // Add a arrow to move section down
echo ''.
' ';
}
}
echo ' | ';
echo ' | ';
}
$section++;
}
echo ' ';
if (!empty($sectionmenu)) {
echo '';
}
if (!empty($THEME->roundcorners)) {
echo ' ';
echo '';
}
echo ' | ';
break;
case 'right':
// The right column
if (blocks_have_content($pageblocks, BLOCK_POS_RIGHT) || $editing) {
echo '';
if (!empty($THEME->roundcorners)) {
echo '';
echo '';
}
blocks_print_group($PAGE, $pageblocks, BLOCK_POS_RIGHT);
if (!empty($THEME->roundcorners)) {
echo ' ';
echo '';
}
echo ' | ';
}
break;
}
}
echo '
';
?>
-----------------------moodle18/course/view.php-----------
libdir.'/blocklib.php');
require_once($CFG->libdir.'/ajax/ajaxlib.php');
require_once($CFG->dirroot.'/mod/forum/lib.php');
$id = optional_param('id', 0, PARAM_INT);
$name = optional_param('name', '', PARAM_RAW);
$edit = optional_param('edit', -1, PARAM_BOOL);
$hide = optional_param('hide', 0, PARAM_INT);
$show = optional_param('show', 0, PARAM_INT);
$idnumber = optional_param('idnumber', '', PARAM_RAW);
$section = optional_param('section', 0, PARAM_INT);
$move = optional_param('move', 0, PARAM_INT);
$marker = optional_param('marker',-1 , PARAM_INT);
$switchrole = optional_param('switchrole',-1, PARAM_INT);
$parent = optional_param('parent','' , PARAM_RAW);
if (empty($id) && empty($name) && empty($idnumber)) {
error("Must specify course id, short name or idnumber");
}
if (!empty($name)) {
if (! ($course = get_record('course', 'shortname', $name)) ) {
error('Invalid short course name');
}
} else if (!empty($idnumber)) {
if (! ($course = get_record('course', 'idnumber', $idnumber)) ) {
error('Invalid course idnumber');
}
} else {
if (! ($course = get_record('course', 'id', $id)) ) {
error('Invalid course id');
}
}
if (!$context = get_context_instance(CONTEXT_COURSE, $course->id)) {
print_error('nocontext');
}
if ($switchrole == 0) { // Remove any switched roles before checking login
role_switch($switchrole, $context);
}
require_login($course->id);
if ($switchrole > 0) {
role_switch($switchrole, $context);
require_login($course->id); // Double check that this role is allowed here
}
//If course is hosted on an external server, redirect to corresponding
//url with appropriate authentication attached as parameter
if (file_exists($CFG->dirroot .'/course/externservercourse.php')) {
include $CFG->dirroot .'/course/externservercourse.php';
if (function_exists('extern_server_course')) {
if ($extern_url = extern_server_course($course)) {
redirect($extern_url);
}
}
}
if (!empty($parent)) {
if (! ($parentcourse = get_record('course', 'id', $parent)) ) {
error('Invalid parent course id');
unset($parent);
} else {
$parentlink = array($parentcourse->shortname => $CFG->wwwroot.'/course/view.php?id='.$parent);
}
}
require_once($CFG->dirroot.'/calendar/lib.php'); /// This is after login because it needs $USER
add_to_log($course->id, 'course', 'view', "view.php?id=$course->id", "$course->id");
$course->format = clean_param($course->format, PARAM_ALPHA);
if (!file_exists($CFG->dirroot.'/course/format/'.$course->format.'/format.php')) {
$course->format = 'weeks'; // Default format is weeks
}
$PAGE = page_create_object(PAGE_COURSE_VIEW, $course->id);
$pageblocks = blocks_setup($PAGE, BLOCKS_PINNED_BOTH);
if (!isset($USER->editing)) {
$USER->editing = 0;
}
if ($PAGE->user_allowed_editing()) {
if (($edit == 1) and confirm_sesskey()) {
$USER->editing = 1;
} else if (($edit == 0) and confirm_sesskey()) {
$USER->editing = 0;
if(!empty($USER->activitycopy) && $USER->activitycopycourse == $course->id) {
$USER->activitycopy = false;
$USER->activitycopycourse = NULL;
}
}
if ($hide && confirm_sesskey()) {
set_section_visible($course->id, $hide, '0');
}
if ($show && confirm_sesskey()) {
set_section_visible($course->id, $show, '1');
}
if (!empty($section)) {
if (!empty($move) and confirm_sesskey()) {
if (!move_section($course, $section, $move)) {
notify('An error occurred while moving a section');
}
}
}
} else {
$USER->editing = 0;
}
$SESSION->fromdiscussion = $CFG->wwwroot .'/course/view.php?id='. $course->id;
if ($course->id == SITEID) {
// This course is not a real course.
redirect($CFG->wwwroot .'/');
}
// AJAX-capable course format?
$CFG->useajax = false;
$ajaxformatfile = $CFG->dirroot.'/course/format/'.$course->format.'/ajax.php';
$bodytags = '';
if (file_exists($ajaxformatfile)) { // Needs to exist otherwise no AJAX by default
$CFG->ajaxcapable = false; // May be overridden later by ajaxformatfile
$CFG->ajaxtestedbrowsers = array(); // May be overridden later by ajaxformatfile
require_once($ajaxformatfile);
if (!empty($USER->editing) && $CFG->ajaxcapable) { // Course-based switches
if (ajaxenabled($CFG->ajaxtestedbrowsers)) { // rowser, user and site-based switches
require_js(array('yui_yahoo',
'yui_dom',
'yui_event',
'yui_dragdrop',
'yui_connection',
'ajaxcourse_blocks',
'ajaxcourse_sections'));
if (debugging('', DEBUG_DEVELOPER)) {
require_js(array('yui_logger'));
$bodytags = 'onload = "javascript:
show_logger = function() {
var logreader = new YAHOO.widget.LogReader();
logreader.newestOnTop = false;
logreader.setTitle(\'Moodle Debug: YUI Log Console\');
};
show_logger();
"';
}
// Okay, global variable alert. VERY UGLY. We need to create
// this object here before the _print_block()
// function is called, since that function needs to set some
// stuff in the javascriptportal object.
$COURSE->javascriptportal = new jsportal();
$CFG->useajax = true;
}
}
}
$CFG->blocksdrag = $CFG->useajax; // this will add a new class to the header so we can style differently
if($parent) {
$PAGE->print_header(get_string('course').': %fullname%', '', '', $bodytags,$parentlink);
} else {
$PAGE->print_header(get_string('course').': %fullname%', '', '', $bodytags);
}
// Course wrapper start.
echo '';
get_all_mods($course->id, $mods, $modnames, $modnamesplural, $modnamesused);
if (! $sections = get_all_sections($course->id)) { // No sections found
// Double-check to be extra sure
if (! $section = get_record('course_sections', 'course', $course->id, 'section', 0)) {
$section->course = $course->id; // Create a default section.
$section->section = 0;
$section->visible = 1;
$section->id = insert_record('course_sections', $section);
}
if (! $sections = get_all_sections($course->id) ) { // Try again
error('Error finding or creating section structures for this course');
}
}
if (empty($course->modinfo)) {
// Course cache was never made.
rebuild_course_cache($course->id);
if (! $course = get_record('course', 'id', $course->id) ) {
error("That's an invalid course id");
}
}
// Include the actual course format.
require($CFG->dirroot .'/course/format/'. $course->format .'/format.php');
// Content wrapper end.
echo "
\n\n";
// Use AJAX?
if ($CFG->useajax) {
// At the bottom because we want to process sections and activities
// after the relevant html has been generated. We're forced to do this
// because of the way in which lib/ajax/ajaxcourse.js is written.
echo '\n";
$COURSE->javascriptportal->print_javascript($course->id);
}
print_footer(NULL, $course);
?>
-----------------------moodle18/course/lib.php-----------
id, '', '', 'u.id, u.firstname, u.lastname')) {
foreach ($courseusers as $courseuser) {
$users[$courseuser->id] = fullname($courseuser, has_capability('moodle/site:viewfullnames', get_context_instance(CONTEXT_COURSE, $course->id)));
}
}
if ($guest = get_guest()) {
$users[$guest->id] = fullname($guest);
}
if (has_capability('moodle/course:update', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
if ($ccc = get_records("course", "", "", "fullname")) {
foreach ($ccc as $cc) {
if ($cc->category) {
$courses["$cc->id"] = "$cc->fullname";
} else {
$courses["$cc->id"] = " $cc->fullname (Site)";
}
}
}
asort($courses);
}
$activities = array();
$selectedactivity = $modid;
/// Casting $course->modinfo to string prevents one notice when the field is null
if ($modinfo = unserialize((string)$course->modinfo)) {
$section = 0;
if ($course->format == 'weeks') { // Body
$strsection = get_string("week");
} else {
$strsection = get_string("topic");
}
$activities["activity/All"] = "All activities";
$activities["activity/Assignments"] = "All assignments";
$activities["activity/Chats"] = "All chats";
$activities["activity/Forums"] = "All forums";
$activities["activity/Quizzes"] = "All quizzes";
$activities["activity/Workshops"] = "All workshops";
$activities["section/individual"] = "------------- Individual Activities --------------";
foreach ($modinfo as $mod) {
if ($mod->mod == "label") {
continue;
}
if (!$mod->visible and !has_capability('moodle/course:viewhiddenactivities',get_context_instance(CONTEXT_MODULE, $mod->cm))) {
continue;
}
if ($mod->section > 0 and $section <> $mod->section) {
$activities["section/$mod->section"] = "-------------- $strsection $mod->section --------------";
}
$section = $mod->section;
$mod->name = strip_tags(format_string(urldecode($mod->name),true));
if (strlen($mod->name) > 55) {
$mod->name = substr($mod->name, 0, 50)."...";
}
if (!$mod->visible) {
$mod->name = "(".$mod->name.")";
}
$activities["$mod->cm"] = $mod->name;
if ($mod->cm == $modid) {
$selectedactivity = "$mod->cm";
}
}
}
$strftimedate = get_string("strftimedate");
$strftimedaydate = get_string("strftimedaydate");
asort($users);
// Get all the possible dates
// Note that we are keeping track of real (GMT) time and user time
// User time is only used in displays - all calcs and passing is GMT
$timenow = time(); // GMT
// What day is it now for the user, and when is midnight that day (in GMT).
$timemidnight = $today = usergetmidnight($timenow);
$dates = array();
$dates["$USER->lastlogin"] = get_string("lastlogin").", ".userdate($USER->lastlogin, $strftimedate);
$dates["$timemidnight"] = get_string("today").", ".userdate($timenow, $strftimedate);
if (!$course->startdate or ($course->startdate > $timenow)) {
$course->startdate = $course->timecreated;
}
$numdates = 1;
while ($timemidnight > $course->startdate and $numdates < 365) {
$timemidnight = $timemidnight - 86400;
$timenow = $timenow - 86400;
$dates["$timemidnight"] = userdate($timenow, $strftimedaydate);
$numdates++;
}
if ($selecteddate === "lastlogin") {
$selecteddate = $USER->lastlogin;
}
echo '";
} else {
$day_list = array("1","7","14","21","30");
$strsince = get_string("since");
$strlastlogin = get_string("lastlogin");
$strday = get_string("day");
$strdays = get_string("days");
$heading = "";
foreach ($day_list as $count) {
if ($count == "1") {
$day = $strday;
} else {
$day = $strdays;
}
$tmpdate = time() - ($count * 3600 * 24);
$heading = $heading .
"wwwroot/course/recent.php?id=$course->id&date=$tmpdate\"> $count $day | ";
}
$heading = $strsince . ": wwwroot/course/recent.php?id=$course->id\">$strlastlogin" . " | " . $heading;
print_heading($heading);
$advancedlink = "wwwroot/course/recent.php?id=$course->id&advancedfilter=1\">" . get_string("advancedfilter") . "";
print_heading($advancedlink);
}
}
function make_log_url($module, $url) {
switch ($module) {
case 'user':
case 'course':
case 'file':
case 'login':
case 'lib':
case 'admin':
case 'message':
case 'calendar':
case 'blog':
return "/$module/$url";
break;
case 'upload':
return $url;
break;
case 'library':
case '':
return '/';
break;
default:
return "/mod/$module/$url";
break;
}
}
function build_mnet_logs_array($hostid, $course, $user=0, $date=0, $order="l.time ASC", $limitfrom='', $limitnum='',
$modname="", $modid=0, $modaction="", $groupid=0) {
global $CFG;
// It is assumed that $date is the GMT time of midnight for that day,
// and so the next 86400 seconds worth of logs are printed.
/// Setup for group handling.
// TODO: I don't understand group/context/etc. enough to be able to do
// something interesting with it here
// What is the context of a remote course?
/// If the group mode is separate, and this user does not have editing privileges,
/// then only the user's group can be viewed.
//if ($course->groupmode == SEPARATEGROUPS and !has_capability('moodle/course:managegroups', get_context_instance(CONTEXT_COURSE, $course->id))) {
// $groupid = get_current_group($course->id);
//}
/// If this course doesn't have groups, no groupid can be specified.
//else if (!$course->groupmode) {
// $groupid = 0;
//}
$groupid = 0;
$joins = array();
$qry = "
SELECT
l.*,
u.firstname,
u.lastname,
u.picture
FROM
{$CFG->prefix}mnet_log l
LEFT JOIN
{$CFG->prefix}user u
ON
l.userid = u.id
WHERE
";
$where .= "l.hostid = '$hostid'";
// TODO: Is 1 really a magic number referring to the sitename?
if ($course != 1 || $modid != 0) {
$where .= " AND\n l.course='$course'";
}
if ($modname) {
$where .= " AND\n l.module = '$modname'";
}
if ('site_errors' === $modid) {
$where .= " AND\n ( l.action='error' OR l.action='infected' )";
} else if ($modid) {
//TODO: This assumes that modids are the same across sites... probably
//not true
$where .= " AND\n l.cmid = '$modid'";
}
if ($modaction) {
$firstletter = substr($modaction, 0, 1);
if (ctype_alpha($firstletter)) {
$where .= " AND\n lower(l.action) LIKE '%" . strtolower($modaction) . "%'";
} else if ($firstletter == '-') {
$where .= " AND\n lower(l.action) NOT LIKE '%" . strtolower(substr($modaction, 1)) . "%'";
}
}
if ($user) {
$where .= " AND\n l.userid = '$user'";
}
if ($date) {
$enddate = $date + 86400;
$where .= " AND\n l.time > '$date' AND l.time < '$enddate'";
}
$result = array();
$result['totalcount'] = count_records_sql("SELECT COUNT(*) FROM {$CFG->prefix}mnet_log l WHERE $where");
if(!empty($result['totalcount'])) {
$where .= "\n ORDER BY\n $order";
$result['logs'] = get_records_sql($qry.$where, $limitfrom, $limitnum);
} else {
$result['logs'] = array();
}
return $result;
}
function build_logs_array($course, $user=0, $date=0, $order="l.time ASC", $limitfrom='', $limitnum='',
$modname="", $modid=0, $modaction="", $groupid=0) {
// It is assumed that $date is the GMT time of midnight for that day,
// and so the next 86400 seconds worth of logs are printed.
/// Setup for group handling.
/// If the group mode is separate, and this user does not have editing privileges,
/// then only the user's group can be viewed.
if ($course->groupmode == SEPARATEGROUPS and !has_capability('moodle/course:managegroups', get_context_instance(CONTEXT_COURSE, $course->id))) {
$groupid = get_current_group($course->id);
}
/// If this course doesn't have groups, no groupid can be specified.
else if (!$course->groupmode) {
$groupid = 0;
}
$joins = array();
if ($course->id != SITEID || $modid != 0) {
$joins[] = "l.course='$course->id'";
}
if ($modname) {
$joins[] = "l.module = '$modname'";
}
if ('site_errors' === $modid) {
$joins[] = "( l.action='error' OR l.action='infected' )";
} else if ($modid) {
$joins[] = "l.cmid = '$modid'";
}
if ($modaction) {
$firstletter = substr($modaction, 0, 1);
if (ctype_alpha($firstletter)) {
$joins[] = "lower(l.action) LIKE '%" . strtolower($modaction) . "%'";
} else if ($firstletter == '-') {
$joins[] = "lower(l.action) NOT LIKE '%" . strtolower(substr($modaction, 1)) . "%'";
}
}
/// Getting all members of a group.
if ($groupid and !$user) {
if ($gusers = groups_get_members($groupid)) { //TODO:check.
$first = true;
foreach($gusers as $guser) {
if ($first) {
$gselect = '(l.userid='.$guser->userid;
$first = false;
}
else {
$gselect .= ' OR l.userid='.$guser->userid;
}
}
if (!$first) $gselect .= ')';
$joins[] = $gselect;
}
}
else if ($user) {
$joins[] = "l.userid = '$user'";
}
if ($date) {
$enddate = $date + 86400;
$joins[] = "l.time > '$date' AND l.time < '$enddate'";
}
$selector = '';
for ($i = 0; $i < count($joins); $i++) {
$selector .= $joins[$i] . (($i == count($joins)-1) ? " " : " AND ");
}
$totalcount = 0; // Initialise
$result = array();
$result['logs'] = get_logs($selector, $order, $limitfrom, $limitnum, $totalcount);
$result['totalcount'] = $totalcount;
return $result;
}
function print_log($course, $user=0, $date=0, $order="l.time ASC", $page=0, $perpage=100,
$url="", $modname="", $modid=0, $modaction="", $groupid=0) {
global $CFG;
if (!$logs = build_logs_array($course, $user, $date, $order, $page*$perpage, $perpage,
$modname, $modid, $modaction, $groupid)) {
notify("No logs found!");
print_footer($course);
exit;
}
$courses = array();
if ($course->id == SITEID) {
$courses[0] = '';
if ($ccc = get_courses('all', 'c.id ASC', 'c.id,c.shortname')) {
foreach ($ccc as $cc) {
$courses[$cc->id] = $cc->shortname;
}
}
} else {
$courses[$course->id] = $course->shortname;
}
$totalcount = $logs['totalcount'];
$count=0;
$ldcache = array();
$tt = getdate(time());
$today = mktime (0, 0, 0, $tt["mon"], $tt["mday"], $tt["year"]);
$strftimedatetime = get_string("strftimedatetime");
echo "\n";
print_string("displayingrecords", "", $totalcount);
echo "
\n";
print_paging_bar($totalcount, $page, $perpage, "$url&perpage=$perpage&");
echo ''."\n";
// echo "\n";
echo "";
if ($course->id == SITEID) {
echo "".get_string('course')." | \n";
}
echo "".get_string('time')." | \n";
echo "".get_string('ip_address')." | \n";
echo "".get_string('fullname')." | \n";
echo "".get_string('action')." | \n";
echo "".get_string('info')." | \n";
echo "
\n";
if (empty($logs['logs'])) {
echo "
\n";
return;
}
$row = 1;
foreach ($logs['logs'] as $log) {
$row = ($row + 1) % 2;
if (isset($ldcache[$log->module][$log->action])) {
$ld = $ldcache[$log->module][$log->action];
} else {
$ld = get_record('log_display', 'module', $log->module, 'action', $log->action);
$ldcache[$log->module][$log->action] = $ld;
}
if ($ld && !empty($log->info)) {
// ugly hack to make sure fullname is shown correctly
if (($ld->mtable == 'user') and ($ld->field == sql_concat('firstname', "' '" , 'lastname'))) {
$log->info = fullname(get_record($ld->mtable, 'id', $log->info), true);
} else {
$log->info = get_field($ld->mtable, $ld->field, 'id', $log->info);
}
}
//Filter log->info
$log->info = format_string($log->info);
$log->url = strip_tags(urldecode($log->url)); // Some XSS protection
$log->info = strip_tags(urldecode($log->info)); // Some XSS protection
$log->url = str_replace('&', '&', $log->url); /// XHTML compatibility
echo '';
if ($course->id == SITEID) {
echo "\n";
echo " wwwroot}/course/view.php?id={$log->course}\">". format_string($courses[$log->course])."\n";
echo " | \n";
}
echo "".userdate($log->time, '%a').
' '.userdate($log->time, $strftimedatetime)." | \n";
echo "\n";
link_to_popup_window("/iplookup/index.php?ip=$log->ip&user=$log->userid", 'iplookup',$log->ip, 400, 700);
echo " | \n";
$fullname = fullname($log, has_capability('moodle/site:viewfullnames', get_context_instance(CONTEXT_COURSE, $course->id)));
echo "\n";
echo " wwwroot/user/view.php?id={$log->userid}&course={$log->course}\">$fullname\n";
echo " | \n";
echo "\n";
link_to_popup_window( make_log_url($log->module,$log->url), 'fromloglive',"$log->module $log->action", 400, 600);
echo " | \n";;
echo "{$log->info} | \n";
echo "
\n";
}
echo "
\n";
print_paging_bar($totalcount, $page, $perpage, "$url&perpage=$perpage&");
}
function print_mnet_log($hostid, $course, $user=0, $date=0, $order="l.time ASC", $page=0, $perpage=100,
$url="", $modname="", $modid=0, $modaction="", $groupid=0) {
global $CFG;
if (!$logs = build_mnet_logs_array($hostid, $course, $user, $date, $order, $page*$perpage, $perpage,
$modname, $modid, $modaction, $groupid)) {
notify("No logs found!");
print_footer($course);
exit;
}
if ($course->id == SITEID) {
$courses[0] = '';
if ($ccc = get_courses('all', 'c.id ASC', 'c.id,c.shortname,c.visible')) {
foreach ($ccc as $cc) {
$courses[$cc->id] = $cc->shortname;
}
}
}
$totalcount = $logs['totalcount'];
$count=0;
$ldcache = array();
$tt = getdate(time());
$today = mktime (0, 0, 0, $tt["mon"], $tt["mday"], $tt["year"]);
$strftimedatetime = get_string("strftimedatetime");
echo "\n";
print_string("displayingrecords", "", $totalcount);
echo "
\n";
print_paging_bar($totalcount, $page, $perpage, "$url&perpage=$perpage&");
echo "\n";
echo "";
if ($course->id == SITEID) {
echo "".get_string('course')." | \n";
}
echo "".get_string('time')." | \n";
echo "".get_string('ip_address')." | \n";
echo "".get_string('fullname')." | \n";
echo "".get_string('action')." | \n";
echo "".get_string('info')." | \n";
echo "
\n";
if (empty($logs['logs'])) {
echo "
\n";
return;
}
$row = 1;
foreach ($logs['logs'] as $log) {
$log->info = $log->coursename;
$row = ($row + 1) % 2;
if (isset($ldcache[$log->module][$log->action])) {
$ld = $ldcache[$log->module][$log->action];
} else {
$ld = get_record('log_display', 'module', $log->module, 'action', $log->action);
$ldcache[$log->module][$log->action] = $ld;
}
if (0 && $ld && !empty($log->info)) {
// ugly hack to make sure fullname is shown correctly
if (($ld->mtable == 'user') and ($ld->field == sql_concat('firstname', "' '" , 'lastname'))) {
$log->info = fullname(get_record($ld->mtable, 'id', $log->info), true);
} else {
$log->info = get_field($ld->mtable, $ld->field, 'id', $log->info);
}
}
//Filter log->info
$log->info = format_string($log->info);
$log->url = strip_tags(urldecode($log->url)); // Some XSS protection
$log->info = strip_tags(urldecode($log->info)); // Some XSS protection
$log->url = str_replace('&', '&', $log->url); /// XHTML compatibility
echo '';
if ($course->id == SITEID) {
echo "\n";
echo " wwwroot}/course/view.php?id={$log->course}\">".$courses[$log->course]."\n";
echo " | \n";
}
echo "".userdate($log->time, '%a').
' '.userdate($log->time, $strftimedatetime)." | \n";
echo "\n";
link_to_popup_window("/iplookup/index.php?ip=$log->ip&user=$log->userid", 'iplookup',$log->ip, 400, 700);
echo " | \n";
$fullname = fullname($log, has_capability('moodle/site:viewfullnames', get_context_instance(CONTEXT_COURSE, $course->id)));
echo "\n";
echo " wwwroot/user/view.php?id={$log->userid}\">$fullname\n";
echo " | \n";
echo "\n";
echo $log->action .': '.$log->module;
echo " | \n";;
echo "{$log->info} | \n";
echo "
\n";
}
echo "\n";
print_paging_bar($totalcount, $page, $perpage, "$url&perpage=$perpage&");
}
function print_log_csv($course, $user, $date, $order='l.time DESC', $modname,
$modid, $modaction, $groupid) {
$text = get_string('course')."\t".get_string('time')."\t".get_string('ip_address')."\t".
get_string('fullname')."\t".get_string('action')."\t".get_string('info');
if (!$logs = build_logs_array($course, $user, $date, $order, '', '',
$modname, $modid, $modaction, $groupid)) {
return false;
}
$courses = array();
if ($course->id == SITEID) {
$courses[0] = '';
if ($ccc = get_courses('all', 'c.id ASC', 'c.id,c.shortname')) {
foreach ($ccc as $cc) {
$courses[$cc->id] = $cc->shortname;
}
}
} else {
$courses[$course->id] = $course->shortname;
}
$count=0;
$ldcache = array();
$tt = getdate(time());
$today = mktime (0, 0, 0, $tt["mon"], $tt["mday"], $tt["year"]);
$strftimedatetime = get_string("strftimedatetime");
$filename = 'logs_'.userdate(time(),get_string('backupnameformat'),99,false);
$filename .= '.txt';
header("Content-Type: application/download\n");
header("Content-Disposition: attachment; filename=$filename");
header("Expires: 0");
header("Cache-Control: must-revalidate,post-check=0,pre-check=0");
header("Pragma: public");
echo get_string('savedat').userdate(time(), $strftimedatetime)."\n";
echo $text;
if (empty($logs['logs'])) {
return true;
}
foreach ($logs['logs'] as $log) {
if (isset($ldcache[$log->module][$log->action])) {
$ld = $ldcache[$log->module][$log->action];
} else {
$ld = get_record('log_display', 'module', $log->module, 'action', $log->action);
$ldcache[$log->module][$log->action] = $ld;
}
if ($ld && !empty($log->info)) {
// ugly hack to make sure fullname is shown correctly
if (($ld->mtable == 'user') and ($ld->field == sql_concat('firstname', "' '" , 'lastname'))) {
$log->info = fullname(get_record($ld->mtable, 'id', $log->info), true);
} else {
$log->info = get_field($ld->mtable, $ld->field, 'id', $log->info);
}
}
//Filter log->info
$log->info = format_string($log->info);
$log->url = strip_tags(urldecode($log->url)); // Some XSS protection
$log->info = strip_tags(urldecode($log->info)); // Some XSS protection
$log->url = str_replace('&', '&', $log->url); // XHTML compatibility
$firstField = $courses[$log->course];
$fullname = fullname($log, has_capability('moodle/site:viewfullnames', get_context_instance(CONTEXT_COURSE, $course->id)));
$row = array($firstField, userdate($log->time, $strftimedatetime), $log->ip, $fullname, $log->module.' '.$log->action, $log->info);
$text = implode("\t", $row);
echo $text." \n";
}
return true;
}
function print_log_xls($course, $user, $date, $order='l.time DESC', $modname,
$modid, $modaction, $groupid) {
global $CFG;
require_once("$CFG->libdir/excellib.class.php");
if (!$logs = build_logs_array($course, $user, $date, $order, '', '',
$modname, $modid, $modaction, $groupid)) {
return false;
}
$courses = array();
if ($course->id == SITEID) {
$courses[0] = '';
if ($ccc = get_courses('all', 'c.id ASC', 'c.id,c.shortname')) {
foreach ($ccc as $cc) {
$courses[$cc->id] = $cc->shortname;
}
}
} else {
$courses[$course->id] = $course->shortname;
}
$count=0;
$ldcache = array();
$tt = getdate(time());
$today = mktime (0, 0, 0, $tt["mon"], $tt["mday"], $tt["year"]);
$strftimedatetime = get_string("strftimedatetime");
$nroPages = ceil(count($logs)/(EXCELROWS-FIRSTUSEDEXCELROW+1));
$filename = 'logs_'.userdate(time(),get_string('backupnameformat'),99,false);
$filename .= '.xls';
$workbook = new MoodleExcelWorkbook('-');
$workbook->send($filename);
$worksheet = array();
$headers = array(get_string('course'), get_string('time'), get_string('ip_address'),
get_string('fullname'), get_string('action'), get_string('info'));
// Creating worksheets
for ($wsnumber = 1; $wsnumber <= $nroPages; $wsnumber++) {
$sheettitle = get_string('excel_sheettitle', 'logs', $wsnumber).$nroPages;
$worksheet[$wsnumber] =& $workbook->add_worksheet($sheettitle);
$worksheet[$wsnumber]->set_column(1, 1, 30);
$worksheet[$wsnumber]->write_string(0, 0, get_string('savedat').
userdate(time(), $strftimedatetime));
$col = 0;
foreach ($headers as $item) {
$worksheet[$wsnumber]->write(FIRSTUSEDEXCELROW-1,$col,$item,'');
$col++;
}
}
if (empty($logs['logs'])) {
$workbook->close();
return true;
}
$formatDate =& $workbook->add_format();
$formatDate->set_num_format(get_string('log_excel_date_format'));
$row = FIRSTUSEDEXCELROW;
$wsnumber = 1;
$myxls =& $worksheet[$wsnumber];
foreach ($logs['logs'] as $log) {
if (isset($ldcache[$log->module][$log->action])) {
$ld = $ldcache[$log->module][$log->action];
} else {
$ld = get_record('log_display', 'module', $log->module, 'action', $log->action);
$ldcache[$log->module][$log->action] = $ld;
}
if ($ld && !empty($log->info)) {
// ugly hack to make sure fullname is shown correctly
if (($ld->mtable == 'user') and ($ld->field == sql_concat('firstname', "' '" , 'lastname'))) {
$log->info = fullname(get_record($ld->mtable, 'id', $log->info), true);
} else {
$log->info = get_field($ld->mtable, $ld->field, 'id', $log->info);
}
}
// Filter log->info
$log->info = format_string($log->info);
$log->info = strip_tags(urldecode($log->info)); // Some XSS protection
if ($nroPages>1) {
if ($row > EXCELROWS) {
$wsnumber++;
$myxls =& $worksheet[$wsnumber];
$row = FIRSTUSEDEXCELROW;
}
}
$myxls->write($row, 0, $courses[$log->course], '');
// Excel counts from 1/1/1900
$excelTime=25569+$log->time/(3600*24);
$myxls->write($row, 1, $excelTime, $formatDate);
$myxls->write($row, 2, $log->ip, '');
$fullname = fullname($log, has_capability('moodle/site:viewfullnames', get_context_instance(CONTEXT_COURSE, $course->id)));
$myxls->write($row, 3, $fullname, '');
$myxls->write($row, 4, $log->module.' '.$log->action, '');
$myxls->write($row, 5, $log->info, '');
$row++;
}
$workbook->close();
return true;
}
function print_log_ods($course, $user, $date, $order='l.time DESC', $modname,
$modid, $modaction, $groupid) {
global $CFG;
require_once("$CFG->libdir/odslib.class.php");
if (!$logs = build_logs_array($course, $user, $date, $order, '', '',
$modname, $modid, $modaction, $groupid)) {
return false;
}
$courses = array();
if ($course->id == SITEID) {
$courses[0] = '';
if ($ccc = get_courses('all', 'c.id ASC', 'c.id,c.shortname')) {
foreach ($ccc as $cc) {
$courses[$cc->id] = $cc->shortname;
}
}
} else {
$courses[$course->id] = $course->shortname;
}
$count=0;
$ldcache = array();
$tt = getdate(time());
$today = mktime (0, 0, 0, $tt["mon"], $tt["mday"], $tt["year"]);
$strftimedatetime = get_string("strftimedatetime");
$nroPages = ceil(count($logs)/(EXCELROWS-FIRSTUSEDEXCELROW+1));
$filename = 'logs_'.userdate(time(),get_string('backupnameformat'),99,false);
$filename .= '.ods';
$workbook = new MoodleODSWorkbook('-');
$workbook->send($filename);
$worksheet = array();
$headers = array(get_string('course'), get_string('time'), get_string('ip_address'),
get_string('fullname'), get_string('action'), get_string('info'));
// Creating worksheets
for ($wsnumber = 1; $wsnumber <= $nroPages; $wsnumber++) {
$sheettitle = get_string('excel_sheettitle', 'logs', $wsnumber).$nroPages;
$worksheet[$wsnumber] =& $workbook->add_worksheet($sheettitle);
$worksheet[$wsnumber]->set_column(1, 1, 30);
$worksheet[$wsnumber]->write_string(0, 0, get_string('savedat').
userdate(time(), $strftimedatetime));
$col = 0;
foreach ($headers as $item) {
$worksheet[$wsnumber]->write(FIRSTUSEDEXCELROW-1,$col,$item,'');
$col++;
}
}
if (empty($logs['logs'])) {
$workbook->close();
return true;
}
$formatDate =& $workbook->add_format();
$formatDate->set_num_format(get_string('log_excel_date_format'));
$row = FIRSTUSEDEXCELROW;
$wsnumber = 1;
$myxls =& $worksheet[$wsnumber];
foreach ($logs['logs'] as $log) {
if (isset($ldcache[$log->module][$log->action])) {
$ld = $ldcache[$log->module][$log->action];
} else {
$ld = get_record('log_display', 'module', $log->module, 'action', $log->action);
$ldcache[$log->module][$log->action] = $ld;
}
if ($ld && !empty($log->info)) {
// ugly hack to make sure fullname is shown correctly
if (($ld->mtable == 'user') and ($ld->field == sql_concat('firstname', "' '" , 'lastname'))) {
$log->info = fullname(get_record($ld->mtable, 'id', $log->info), true);
} else {
$log->info = get_field($ld->mtable, $ld->field, 'id', $log->info);
}
}
// Filter log->info
$log->info = format_string($log->info);
$log->info = strip_tags(urldecode($log->info)); // Some XSS protection
if ($nroPages>1) {
if ($row > EXCELROWS) {
$wsnumber++;
$myxls =& $worksheet[$wsnumber];
$row = FIRSTUSEDEXCELROW;
}
}
$myxls->write_string($row, 0, $courses[$log->course]);
$myxls->write_date($row, 1, $log->time);
$myxls->write_string($row, 2, $log->ip);
$fullname = fullname($log, has_capability('moodle/site:viewfullnames', get_context_instance(CONTEXT_COURSE, $course->id)));
$myxls->write_string($row, 3, $fullname);
$myxls->write_string($row, 4, $log->module.' '.$log->action);
$myxls->write_string($row, 5, $log->info);
$row++;
}
$workbook->close();
return true;
}
function print_log_graph($course, $userid=0, $type="course.png", $date=0) {
global $CFG;
if (empty($CFG->gdversion)) {
echo "(".get_string("gdneed").")";
} else {
echo '
';
}
}
function print_overview($courses) {
global $CFG, $USER;
$htmlarray = array();
if ($modules = get_records('modules')) {
foreach ($modules as $mod) {
if (file_exists(dirname(dirname(__FILE__)).'/mod/'.$mod->name.'/lib.php')) {
require_once(dirname(dirname(__FILE__)).'/mod/'.$mod->name.'/lib.php');
$fname = $mod->name.'_print_overview';
if (function_exists($fname)) {
$fname($courses,$htmlarray);
}
}
}
}
foreach ($courses as $course) {
print_simple_box_start('center', '100%', '', 5, "coursebox");
$linkcss = '';
if (empty($course->visible)) {
$linkcss = 'class="dimmed"';
}
print_heading(''. format_string($course->fullname).'');
if (array_key_exists($course->id,$htmlarray)) {
foreach ($htmlarray[$course->id] as $modname => $html) {
echo $html;
}
}
print_simple_box_end();
}
}
function print_recent_activity($course) {
// $course is an object
// This function trawls through the logs looking for
// anything new since the user's last login
global $CFG, $USER, $SESSION;
$context = get_context_instance(CONTEXT_COURSE, $course->id);
$timestart = time() - COURSE_MAX_RECENT_PERIOD;
if (!has_capability('moodle/legacy:guest', $context, NULL, false)) {
if (!empty($USER->lastcourseaccess[$course->id])) {
if ($USER->lastcourseaccess[$course->id] > $timestart) {
$timestart = $USER->lastcourseaccess[$course->id];
}
}
}
echo '';
echo get_string('activitysince', '', userdate($timestart));
echo '
';
echo '\n";
// Firstly, have there been any new enrolments?
$heading = false;
$content = false;
$users = get_recent_enrolments($course->id, $timestart);
//Accessibility: new users now appear in an list.
if ($users) {
echo '';
if (! $heading) {
print_headline(get_string("newusers").':', 3);
$heading = true;
$content = true;
}
echo "
\n";
foreach ($users as $user) {
$fullname = fullname($user, has_capability('moodle/site:viewfullnames', get_context_instance(CONTEXT_COURSE, $course->id)));
echo '- id&course=$course->id\">$fullname
\n";
}
echo "
\n
\n";
}
// Next, have there been any modifications to the course structure?
$logs = get_records_select('log', "time > '$timestart' AND course = '$course->id' AND
module = 'course' AND action LIKE '% mod'", "time ASC");
if ($logs) {
foreach ($logs as $key => $log) {
$info = split(' ', $log->info);
if ($info[0] == 'label') { // Labels are special activities
continue;
}
$modname = get_field($info[0], 'name', 'id', $info[1]);
//Create a temp valid module structure (course,id)
$tempmod->course = $log->course;
$tempmod->id = $info[1];
//Obtain the visible property from the instance
$modvisible = instance_is_visible($info[0],$tempmod);
//Only if the mod is visible
if ($modvisible) {
switch ($log->action) {
case 'add mod':
$stradded = get_string('added', 'moodle', get_string('modulename', $info[0]));
$changelist[$log->info] = array ('operation' => 'add', 'text' => "$stradded:
wwwroot/course/$log->url\">".format_string($modname,true)."");
break;
case 'update mod':
$strupdated = get_string('updated', 'moodle', get_string('modulename', $info[0]));
if (empty($changelist[$log->info])) {
$changelist[$log->info] = array ('operation' => 'update', 'text' => "$strupdated:
wwwroot/course/$log->url\">".format_string($modname,true)."");
}
break;
case 'delete mod':
if (!empty($changelist[$log->info]['operation']) and
$changelist[$log->info]['operation'] == 'add') {
$changelist[$log->info] = NULL;
} else {
$strdeleted = get_string('deletedactivity', 'moodle', get_string('modulename', $info[0]));
$changelist[$log->info] = array ('operation' => 'delete', 'text' => $strdeleted);
}
break;
}
}
}
}
if (!empty($changelist)) {
foreach ($changelist as $changeinfo => $change) {
if ($change) {
$changes[$changeinfo] = $change;
}
}
if (isset($changes)){
if (count($changes) > 0) {
print_headline(get_string('courseupdates').':', 3);
$content = true;
foreach ($changes as $changeinfo => $change) {
echo ''.$change['text'].'
';
}
}
}
}
// Now display new things from each module
$mods = get_records('modules', 'visible', '1', 'name', 'id, name');
$viewfullnames = has_capability('moodle/site:viewfullnames', $context);
foreach ($mods as $mod) { // Each module gets it's own logs and prints them
include_once($CFG->dirroot.'/mod/'.$mod->name.'/lib.php');
$print_recent_activity = $mod->name.'_print_recent_activity';
if (function_exists($print_recent_activity)) {
//
// NOTE:
// $isteacher (second parameter below) is to be deprecated!
//
// TODO:
// 1) Make sure that all _print_recent_activity functions are
// not using the $isteacher value.
// 2) Eventually, remove the $isteacher parameter from the
// function calls.
//
$modcontent = $print_recent_activity($course, $viewfullnames, $timestart);
if ($modcontent) {
$content = true;
}
}
}
if (! $content) {
echo ''.get_string('nothingnew').'
';
}
}
function get_array_of_activities($courseid) {
// For a given course, returns an array of course activity objects
// Each item in the array contains he following properties:
// cm - course module id
// mod - name of the module (eg forum)
// section - the number of the section (eg week or topic)
// name - the name of the instance
// visible - is the instance visible or not
// extra - contains extra string to include in any link
global $CFG;
$mod = array();
if (!$rawmods = get_course_mods($courseid)) {
return NULL;
}
if ($sections = get_records("course_sections", "course", $courseid, "section ASC")) {
foreach ($sections as $section) {
if (!empty($section->sequence)) {
$sequence = explode(",", $section->sequence);
foreach ($sequence as $seq) {
if (empty($rawmods[$seq])) {
continue;
}
$mod[$seq]->cm = $rawmods[$seq]->id;
$mod[$seq]->mod = $rawmods[$seq]->modname;
$mod[$seq]->section = $section->section;
$mod[$seq]->name = urlencode(get_field($rawmods[$seq]->modname, "name", "id", $rawmods[$seq]->instance));
$mod[$seq]->visible = $rawmods[$seq]->visible;
$mod[$seq]->extra = "";
$modname = $mod[$seq]->mod;
$functionname = $modname."_get_coursemodule_info";
include_once("$CFG->dirroot/mod/$modname/lib.php");
if (function_exists($functionname)) {
if ($info = $functionname($rawmods[$seq])) {
if (!empty($info->extra)) {
$mod[$seq]->extra = $info->extra;
}
if (!empty($info->icon)) {
$mod[$seq]->icon = $info->icon;
}
}
}
}
}
}
}
return $mod;
}
function get_all_mods($courseid, &$mods, &$modnames, &$modnamesplural, &$modnamesused) {
// Returns a number of useful structures for course displays
$mods = NULL; // course modules indexed by id
$modnames = NULL; // all course module names (except resource!)
$modnamesplural= NULL; // all course module names (plural form)
$modnamesused = NULL; // course module names used
if ($allmods = get_records("modules")) {
foreach ($allmods as $mod) {
if ($mod->visible) {
$modnames[$mod->name] = get_string("modulename", "$mod->name");
$modnamesplural[$mod->name] = get_string("modulenameplural", "$mod->name");
}
}
asort($modnames);
} else {
error("No modules are installed!");
}
if ($rawmods = get_course_mods($courseid)) {
foreach($rawmods as $mod) { // Index the mods
if (empty($modnames[$mod->modname])) {
continue;
}
$mods[$mod->id] = $mod;
$mods[$mod->id]->modfullname = $modnames[$mod->modname];
if ($mod->visible or has_capability('moodle/course:viewhiddenactivities', get_context_instance(CONTEXT_COURSE, $courseid))) {
$modnamesused[$mod->modname] = $modnames[$mod->modname];
}
}
if ($modnamesused) {
asort($modnamesused);
}
}
}
function get_all_sections($courseid) {
return get_records("course_sections", "course", "$courseid", "section",
"section, id, course, summary, sequence, visible");
}
function course_set_display($courseid, $display=0) {
global $USER;
if ($display == "all" or empty($display)) {
$display = 0;
}
if (empty($USER->id) or $USER->username == 'guest') {
//do not store settings in db for guests
} else if (record_exists("course_display", "userid", $USER->id, "course", $courseid)) {
set_field("course_display", "display", $display, "userid", $USER->id, "course", $courseid);
} else {
$record->userid = $USER->id;
$record->course = $courseid;
$record->display = $display;
if (!insert_record("course_display", $record)) {
notify("Could not save your course display!");
}
}
return $USER->display[$courseid] = $display; // Note: = not ==
}
function set_section_visible($courseid, $sectionnumber, $visibility) {
/// For a given course section, markes it visible or hidden,
/// and does the same for every activity in that section
if ($section = get_record("course_sections", "course", $courseid, "section", $sectionnumber)) {
set_field("course_sections", "visible", "$visibility", "id", $section->id);
if (!empty($section->sequence)) {
$modules = explode(",", $section->sequence);
foreach ($modules as $moduleid) {
set_coursemodule_visible($moduleid, $visibility, true);
}
}
rebuild_course_cache($courseid);
}
}
function print_section($course, $section, $mods, $modnamesused, $absolute=false, $width="100%") {
/// Prints a section full of activity modules
global $CFG, $USER;
static $groupbuttons;
static $groupbuttonslink;
static $isteacher;
static $isediting;
static $ismoving;
static $strmovehere;
static $strmovefull;
static $strunreadpostsone;
static $untracked;
static $usetracking;
$labelformatoptions = New stdClass;
if (!isset($isteacher)) {
$groupbuttons = ($course->groupmode or (!$course->groupmodeforce));
$groupbuttonslink = (!$course->groupmodeforce);
$isediting = isediting($course->id);
$ismoving = $isediting && ismoving($course->id);
if ($ismoving) {
$strmovehere = get_string("movehere");
$strmovefull = strip_tags(get_string("movefull", "", "'$USER->activitycopyname'"));
}
include_once($CFG->dirroot.'/mod/forum/lib.php');
if ($usetracking = forum_tp_can_track_forums()) {
$strunreadpostsone = get_string('unreadpostsone', 'forum');
$untracked = forum_tp_get_untracked_forums($USER->id, $course->id);
}
}
$labelformatoptions->noclean = true;
/// Casting $course->modinfo to string prevents one notice when the field is null
$modinfo = unserialize((string)$course->modinfo);
//Acccessibility: replace table with list
';
} else if ($format == 'short') {
$output = '';
} else if ($format == 'navbar') {
$output = '';
}
if ($return) {
return $output;
}
echo $output;
}
function print_remote_course($course, $width="100%") {
global $CFG, $USER;
$linkcss = '';
$url = "{$CFG->wwwroot}/auth/mnet/jump.php?hostid={$course->hostid}&wantsurl=/course/view.php?id={$course->remoteid}";
echo '';
echo '
';
$options = NULL;
$options->noclean = true;
$options->para = false;
echo format_text($course->summary, FORMAT_MOODLE, $options);
echo '
';
echo '
';
echo '';
}
function print_remote_host($host, $width="100%") {
global $CFG, $USER;
$linkcss = '';
echo '';
echo '';
}
/// MODULE FUNCTIONS /////////////////////////////////////////////////////////////////
function add_course_module($mod) {
$mod->added = time();
unset($mod->id);
return insert_record("course_modules", $mod);
}
function add_mod_to_section($mod, $beforemod=NULL) {
/// Given a full mod object with section and course already defined
/// If $before is specified, then this is an existing ID which we
/// will insert the new module before
///
/// Returns the course_sections ID where the mod is inserted
if ($section = get_record("course_sections", "course", "$mod->course", "section", "$mod->section")) {
$section->sequence = trim($section->sequence);
if (empty($section->sequence)) {
$newsequence = "$mod->coursemodule";
} else if ($beforemod) {
$modarray = explode(",", $section->sequence);
if ($key = array_keys ($modarray, $beforemod->id)) {
$insertarray = array($mod->id, $beforemod->id);
array_splice($modarray, $key[0], 1, $insertarray);
$newsequence = implode(",", $modarray);
} else { // Just tack it on the end anyway
$newsequence = "$section->sequence,$mod->coursemodule";
}
} else {
$newsequence = "$section->sequence,$mod->coursemodule";
}
if (set_field("course_sections", "sequence", $newsequence, "id", $section->id)) {
return $section->id; // Return course_sections ID that was used.
} else {
return 0;
}
} else { // Insert a new record
$section->course = $mod->course;
$section->section = $mod->section;
$section->summary = "";
$section->sequence = $mod->coursemodule;
return insert_record("course_sections", $section);
}
}
function set_coursemodule_groupmode($id, $groupmode) {
return set_field("course_modules", "groupmode", $groupmode, "id", $id);
}
/**
* $prevstateoverrides = true will set the visibility of the course module
* to what is defined in visibleold. This enables us to remember the current
* visibility when making a whole section hidden, so that when we toggle
* that section back to visible, we are able to return the visibility of
* the course module back to what it was originally.
*/
function set_coursemodule_visible($id, $visible, $prevstateoverrides=false) {
if (!$cm = get_record('course_modules', 'id', $id)) {
return false;
}
if (!$modulename = get_field('modules', 'name', 'id', $cm->module)) {
return false;
}
if ($events = get_records_select('event', "instance = '$cm->instance' AND modulename = '$modulename'")) {
foreach($events as $event) {
if ($visible) {
show_event($event);
} else {
hide_event($event);
}
}
}
if ($prevstateoverrides) {
if ($visible == '0') {
// Remember the current visible state so we can toggle this back.
set_field('course_modules', 'visibleold', $cm->visible, 'id', $id);
} else {
// Get the previous saved visible states.
return set_field('course_modules', 'visible', $cm->visibleold, 'id', $id);
}
}
return set_field("course_modules", "visible", $visible, "id", $id);
}
/*
* Delete a course module and any associated data at the course level (events)
* Until 1.5 this function simply marked a deleted flag ... now it
* deletes it completely.
*
*/
function delete_course_module($id) {
if (!$cm = get_record('course_modules', 'id', $id)) {
return true;
}
$modulename = get_field('modules', 'name', 'id', $cm->module);
if ($events = get_records_select('event', "instance = '$cm->instance' AND modulename = '$modulename'")) {
foreach($events as $event) {
delete_event($event);
}
}
return delete_records('course_modules', 'id', $cm->id);
}
function delete_mod_from_section($mod, $section) {
if ($section = get_record("course_sections", "id", "$section") ) {
$modarray = explode(",", $section->sequence);
if ($key = array_keys ($modarray, $mod)) {
array_splice($modarray, $key[0], 1);
$newsequence = implode(",", $modarray);
return set_field("course_sections", "sequence", $newsequence, "id", $section->id);
} else {
return false;
}
}
return false;
}
function move_section($course, $section, $move) {
/// Moves a whole course section up and down within the course
global $USER;
if (!$move) {
return true;
}
$sectiondest = $section + $move;
if ($sectiondest > $course->numsections or $sectiondest < 1) {
return false;
}
if (!$sectionrecord = get_record("course_sections", "course", $course->id, "section", $section)) {
return false;
}
if (!$sectiondestrecord = get_record("course_sections", "course", $course->id, "section", $sectiondest)) {
return false;
}
if (!set_field("course_sections", "section", $sectiondest, "id", $sectionrecord->id)) {
return false;
}
if (!set_field("course_sections", "section", $section, "id", $sectiondestrecord->id)) {
return false;
}
// if the focus is on the section that is being moved, then move the focus along
if (isset($USER->display[$course->id]) and ($USER->display[$course->id] == $section)) {
course_set_display($course->id, $sectiondest);
}
// Check for duplicates and fix order if needed.
// There is a very rare case that some sections in the same course have the same section id.
$sections = get_records_select('course_sections', "course = $course->id", 'section ASC');
$n = 0;
foreach ($sections as $section) {
if ($section->section != $n) {
if (!set_field('course_sections', 'section', $n, 'id', $section->id)) {
return false;
}
}
$n++;
}
return true;
}
function moveto_module($mod, $section, $beforemod=NULL) {
/// All parameters are objects
/// Move the module object $mod to the specified $section
/// If $beforemod exists then that is the module
/// before which $modid should be inserted
/// Remove original module from original section
if (! delete_mod_from_section($mod->id, $mod->section)) {
notify("Could not delete module from existing section");
}
/// Update module itself if necessary
if ($mod->section != $section->id) {
$mod->section = $section->id;
if (!update_record("course_modules", $mod)) {
return false;
}
// if moving to a hidden section then hide module
if (!$section->visible) {
set_coursemodule_visible($mod->id, 0);
}
}
/// Add the module into the new section
$mod->course = $section->course;
$mod->section = $section->section; // need relative reference
$mod->coursemodule = $mod->id;
if (! add_mod_to_section($mod, $beforemod)) {
return false;
}
return true;
}
function make_editing_buttons($mod, $absolute=false, $moveselect=true, $indent=-1, $section=-1) {
global $CFG, $USER;
static $str;
static $sesskey;
$modcontext = get_context_instance(CONTEXT_MODULE, $mod->id);
// no permission to edit
if (!has_capability('moodle/course:manageactivities', $modcontext)) {
return false;
}
if (!isset($str)) {
$str->delete = get_string("delete");
$str->move = get_string("move");
$str->moveup = get_string("moveup");
$str->movedown = get_string("movedown");
$str->moveright = get_string("moveright");
$str->moveleft = get_string("moveleft");
$str->update = get_string("update");
$str->duplicate = get_string("duplicate");
$str->hide = get_string("hide");
$str->show = get_string("show");
$str->clicktochange = get_string("clicktochange");
$str->forcedmode = get_string("forcedmode");
$str->groupsnone = get_string("groupsnone");
$str->groupsseparate = get_string("groupsseparate");
$str->groupsvisible = get_string("groupsvisible");
$sesskey = sesskey();
}
if ($section >= 0) {
$section = '&sr='.$section; // Section return
} else {
$section = '';
}
if ($absolute) {
$path = $CFG->wwwroot.'/course';
} else {
$path = '.';
}
if ($mod->visible) {
$hideshow = '
'."\n";
} else {
$hideshow = '
'."\n";
}
if ($mod->groupmode !== false) {
if ($mod->groupmode == SEPARATEGROUPS) {
$grouptitle = $str->groupsseparate;
$groupclass = 'editing_groupsseparate';
$groupimage = $CFG->pixpath.'/t/groups.gif';
$grouplink = $path.'/mod.php?id='.$mod->id.'&groupmode=0&sesskey='.$sesskey;
} else if ($mod->groupmode == VISIBLEGROUPS) {
$grouptitle = $str->groupsvisible;
$groupclass = 'editing_groupsvisible';
$groupimage = $CFG->pixpath.'/t/groupv.gif';
$grouplink = $path.'/mod.php?id='.$mod->id.'&groupmode=1&sesskey='.$sesskey;
} else {
$grouptitle = $str->groupsnone;
$groupclass = 'editing_groupsnone';
$groupimage = $CFG->pixpath.'/t/groupn.gif';
$grouplink = $path.'/mod.php?id='.$mod->id.'&groupmode=2&sesskey='.$sesskey;
}
if ($mod->groupmodelink) {
$groupmode = ''.
'
';
} else {
$groupmode = '
';
}
} else {
$groupmode = "";
}
if (has_capability('moodle/course:update', get_context_instance(CONTEXT_COURSE, $mod->course))) {
if ($moveselect) {
$move = '
'."\n";
} else {
$move = '
'."\n".
'
'."\n";
}
}
$leftright = "";
if ($indent > 0) {
$leftright .= '
'."\n";
}
if ($indent >= 0) {
$leftright .= '
'."\n";
}
return ''."\n".$leftright.$move.
'
'."\n".
'
'."\n".$hideshow.$groupmode."\n".'';
}
/**
* given a course object with shortname & fullname, this function will
* truncate the the number of chars allowed and add ... if it was too long
*/
function course_format_name ($course,$max=100) {
$str = $course->shortname.': '. $course->fullname;
if (strlen($str) <= $max) {
return $str;
}
else {
return substr($str,0,$max-3).'...';
}
}
/**
* This function will return true if the given course is a child course at all
*/
function course_in_meta ($course) {
return record_exists("course_meta","child_course",$course->id);
}
/**
* Print standard form elements on module setup forms in mod/.../mod.html
*/
function print_standard_coursemodule_settings($form) {
if (! $course = get_record('course', 'id', $form->course)) {
error("This course doesn't exist");
}
print_groupmode_setting($form, $course);
print_visible_setting($form, $course);
}
/**
* Print groupmode form element on module setup forms in mod/.../mod.html
*/
function print_groupmode_setting($form, $course=NULL) {
if (empty($course)) {
if (! $course = get_record('course', 'id', $form->course)) {
error("This course doesn't exist");
}
}
if ($form->coursemodule) {
if (! $cm = get_record('course_modules', 'id', $form->coursemodule)) {
error("This course module doesn't exist");
}
} else {
$cm = null;
}
$groupmode = groupmode($course, $cm);
if ($course->groupmode or (!$course->groupmodeforce)) {
echo '';
echo ''.get_string('groupmode').': | ';
echo '';
unset($choices);
$choices[NOGROUPS] = get_string('groupsnone');
$choices[SEPARATEGROUPS] = get_string('groupsseparate');
$choices[VISIBLEGROUPS] = get_string('groupsvisible');
choose_from_menu($choices, 'groupmode', $groupmode, '', '', 0, false, $course->groupmodeforce);
helpbutton('groupmode', get_string('groupmode'));
echo ' |
';
}
}
/**
* Print visibility setting form element on module setup forms in mod/.../mod.html
*/
function print_visible_setting($form, $course=NULL) {
if (empty($course)) {
if (! $course = get_record('course', 'id', $form->course)) {
error("This course doesn't exist");
}
}
if ($form->coursemodule) {
$visible = get_field('course_modules', 'visible', 'id', $form->coursemodule);
} else {
$visible = true;
}
if ($form->mode == 'add') { // in this case $form->section is the section number, not the id
$hiddensection = !get_field('course_sections', 'visible', 'section', $form->section, 'course', $form->course);
} else {
$hiddensection = !get_field('course_sections', 'visible', 'id', $form->section);
}
if ($hiddensection) {
$visible = false;
}
echo '';
echo ''.get_string('visible', '').': | ';
echo '';
unset($choices);
$choices[1] = get_string('show');
$choices[0] = get_string('hide');
choose_from_menu($choices, 'visible', $visible, '', '', 0, false, $hiddensection);
echo ' |
';
}
function update_restricted_mods($course,$mods) {
delete_records("course_allowed_modules","course",$course->id);
if (empty($course->restrictmodules)) {
return;
}
else {
foreach ($mods as $mod) {
if ($mod == 0)
continue; // this is the 'allow none' option
$am->course = $course->id;
$am->module = $mod;
insert_record("course_allowed_modules",$am);
}
}
}
/**
* This function will take an int (module id) or a string (module name)
* and return true or false, whether it's allowed in the given course (object)
* $mod is not allowed to be an object, as the field for the module id is inconsistent
* depending on where in the code it's called from (sometimes $mod->id, sometimes $mod->module)
*/
function course_allowed_module($course,$mod) {
if (empty($course->restrictmodules)) {
return true;
}
// i am not sure this capability is correct
if (has_capability('moodle/course:update', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
return true;
}
if (is_numeric($mod)) {
$modid = $mod;
} else if (is_string($mod)) {
if ($mod = get_field("modules","id","name",$mod))
$modid = $mod;
}
if (empty($modid)) {
return false;
}
return (record_exists("course_allowed_modules","course",$course->id,"module",$modid));
}
/***
*** Efficiently moves many courses around while maintaining
*** sortorder in order.
***
*** $courseids is an array of course ids
***
**/
function move_courses ($courseids, $categoryid) {
global $CFG;
if (!empty($courseids)) {
$courseids = array_reverse($courseids);
foreach ($courseids as $courseid) {
if (! $course = get_record("course", "id", $courseid)) {
notify("Error finding course $courseid");
} else {
// figure out a sortorder that we can use in the destination category
$sortorder = get_field_sql('SELECT MIN(sortorder)-1 AS min
FROM ' . $CFG->prefix . 'course WHERE category=' . $categoryid);
if ($sortorder === false) {
// the category is empty
// rather than let the db default to 0
// set it to > 100 and avoid extra work in fix_coursesortorder()
$sortorder = 200;
} else if ($sortorder < 10) {
fix_course_sortorder($categoryid);
}
$course->category = $categoryid;
$course->sortorder = $sortorder;
$course->fullname = addslashes($course->fullname);
$course->shortname = addslashes($course->shortname);
$course->summary = addslashes($course->summary);
$course->password = addslashes($course->password);
$course->teacher = addslashes($course->teacher);
$course->teachers = addslashes($course->teachers);
$course->student = addslashes($course->student);
$course->students = addslashes($course->students);
if (!update_record('course', $course)) {
notify("An error occurred - course not moved!");
}
// parents changed (course category), do not delete child context relations
insert_context_rel(get_context_instance(CONTEXT_COURSE, $course->id), false);
}
}
fix_course_sortorder();
}
return true;
}
/**
* @param string $format Course format ID e.g. 'weeks'
* @return Name that the course format prefers for sections
*/
function get_section_name($format) {
$sectionname = get_string("name$format","format_$format");
if($sectionname == "[[name$format]]") {
$sectionname = get_string("name$format");
}
return $sectionname;
}
/**
* Can the current user delete this course?
* @param int $courseid
* @return boolean
*
* Exception here to fix MDL-7796.
*
* FIXME
* Course creators who can manage activities in the course
* shoule be allowed to delete the course. We do it this
* way because we need a quick fix to bring the functionality
* in line with what we had pre-roles. We can't give the
* default course creator role moodle/course:delete at
* CONTEXT_SYSTEM level because this will allow them to
* delete any course in the site. So we hard code this here
* for now.
*
* @author vyshane AT gmail.com
*/
function can_delete_course($courseid) {
$context = get_context_instance(CONTEXT_COURSE, $courseid);
return ( has_capability('moodle/course:delete', $context)
|| (has_capability('moodle/legacy:coursecreator', $context)
&& has_capability('moodle/course:manageactivities', $context)) );
}
/*
* Create a course and either return a $course object or false
*
* @param object $data - all the data needed for an entry in the 'course' table
*/
function create_course($data) {
global $CFG, $USER;
// preprocess allowed mods
$allowedmods = empty($data->allowedmods) ? array() : $data->allowedmods;
unset($data->allowedmods);
if (!has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM))) {
if ($CFG->restrictmodulesfor == 'all') {
$data->restrictmodules = 1;
} else {
$data->restrictmodules = 0;
}
}
$data->timecreated = time();
// place at beginning of category
fix_course_sortorder();
$data->sortorder = get_field_sql("SELECT min(sortorder)-1 FROM {$CFG->prefix}course WHERE category=$data->category");
if (empty($data->sortorder)) {
$data->sortorder = 100;
}
if ($newcourseid = insert_record('course', $data)) { // Set up new course
$course = get_record('course', 'id', $newcourseid);
// Setup the blocks
$page = page_create_object(PAGE_COURSE_VIEW, $course->id);
blocks_repopulate_page($page); // Return value not checked because you can always edit later
if (has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM))) {
update_restricted_mods($course, $allowedmods);
}
$section = new object();
$section->course = $course->id; // Create a default section.
$section->section = 0;
$section->id = insert_record('course_sections', $section);
fix_course_sortorder();
add_to_log(SITEID, 'course', 'new', 'view.php?id='.$course->id, $data->fullname.' (ID '.$course->id.')');
return $course;
}
return false; // error
}
/*
* Update a course and return true or false
*
* @param object $data - all the data needed for an entry in the 'course' table
*/
function update_course($data) {
global $USER, $CFG;
// preprocess allowed mods
$allowedmods = empty($data->allowedmods) ? array() : $data->allowedmods;
unset($data->allowedmods);
if (!has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM))) {
unset($data->restrictmodules);
}
$oldcourse = get_record('course', 'id', $data->id); // should not fail, already tested above
if (!has_capability('moodle/course:create', get_context_instance(CONTEXT_COURSECAT, $oldcourse->category))
or !has_capability('moodle/course:create', get_context_instance(CONTEXT_COURSECAT, $data->category))) {
// can not move to new category, keep the old one
unset($data->category);
}
// Update with the new data
if (update_record('course', $data)) {
$course = get_record('course', 'id', $data->id);
add_to_log($course->id, "course", "update", "edit.php?id=$course->id", "");
if (has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM))) {
update_restricted_mods($course, $allowedmods);
}
fix_course_sortorder();
// Test for and remove blocks which aren't appropriate anymore
$page = page_create_object(PAGE_COURSE_VIEW, $course->id);
blocks_remove_inappropriate($page);
return true;
}
return false;
}
?>
-----------------------moodle18/lib/pagelib.php-----------***********NEW CHANGES************
dirroot.'/'.$path.'pagelib.php';
if(is_file($file)) {
require($file);
if(!isset($DEFINEDPAGES)) {
error('Imported '.$file.' but found no page classes');
}
return $types[$path] = $DEFINEDPAGES;
}
return false;
}
/**
* Factory function page_create_object(). Called with a numeric ID for a page, it autodetects
* the page type, constructs the correct object and returns it.
*/
function page_create_instance($instance) {
page_id_and_class($id, $class);
return page_create_object($id, $instance);
}
/**
* Factory function page_create_object(). Called with a pagetype identifier and possibly with
* its numeric ID. Returns a fully constructed page_base subclass you can work with.
*/
function page_create_object($type, $id = NULL) {
global $CFG;
$data = new stdClass;
$data->pagetype = $type;
$data->pageid = $id;
$classname = page_map_class($type);
$object = &new $classname;
// TODO: subclassing check here
if ($object->get_type() !== $type) {
// Somehow somewhere someone made a mistake
debugging('Page object\'s type ('. $object->get_type() .') does not match requested type ('. $type .')');
}
$object->init_quick($data);
return $object;
}
/**
* Function page_map_class() is the way for your code to define its own page subclasses and let Moodle recognize them.
* Use it to associate the textual identifier of your Page with the actual class name that has to be instantiated.
*/
function page_map_class($type, $classname = NULL) {
global $CFG;
static $mappings = NULL;
if ($mappings === NULL) {
$mappings = array(
PAGE_COURSE_VIEW => 'page_course'
);
}
if (!empty($type) && !empty($classname)) {
$mappings[$type] = $classname;
}
if (!isset($mappings[$type])) {
debugging('Page class mapping requested for unknown type: '.$type);
}
if (empty($classname) && !class_exists($mappings[$type])) {
debugging('Page class mapping for id "'.$type.'" exists but class "'.$mappings[$type].'" is not defined');
}
return $mappings[$type];
}
/**
* Parent class from which all Moodle page classes derive
*
* @author Jon Papaioannou
* @package pages
* @todo This parent class is very messy still. Please for the moment ignore it and move on to the derived class page_course to see the comments there.
*/
class page_base {
/**
* The string identifier for the type of page being described.
* @var string $type
*/
var $type = NULL;
/**
* The numeric identifier of the page being described.
* @var int $id
*/
var $id = NULL;
/**
* Class bool to determine if the instance's full initialization has been completed.
* @var boolean $full_init_done
*/
var $full_init_done = false;
/**
* The class attribute that Moodle has to assign to the BODY tag for this page.
* @var string $body_class
*/
var $body_class = NULL;
/**
* The id attribute that Moodle has to assign to the BODY tag for this page.
* @var string $body_id
*/
var $body_id = NULL;
/// Class Functions
// CONSTRUCTION
// A whole battery of functions to allow standardized-name constructors in all versions of PHP.
// The constructor is actually called construct()
function page_base() {
$this->construct();
}
function __construct() {
$this->construct();
}
function construct() {
page_id_and_class($this->body_id, $this->body_class);
}
// USER-RELATED THINGS
// By default, no user is editing anything and none CAN edit anything. Developers
// will have to override these settings to let Moodle know when it should grant
// editing rights to the user viewing the page.
function user_allowed_editing() {
trigger_error('Page class does not implement method user_allowed_editing()', E_USER_WARNING);
return false;
}
function user_is_editing() {
trigger_error('Page class does not implement method user_is_editing()', E_USER_WARNING);
return false;
}
// HTML OUTPUT SECTION
// We have absolutely no idea what derived pages are all about
function print_header($title, $morebreadcrumbs) {
trigger_error('Page class does not implement method print_header()', E_USER_WARNING);
return;
}
// BLOCKS RELATED SECTION
// By default, pages don't have any blocks. Override this in your derived class if you need blocks.
function blocks_get_positions() {
return array();
}
// Thus there is no default block position. If you override the above you should override this one too.
// Because this makes sense only if blocks_get_positions() is overridden and because these two should
// be overridden as a group or not at all, this one issues a warning. The sneaky part is that this warning
// will only be seen if you override blocks_get_positions() but NOT blocks_default_position().
function blocks_default_position() {
trigger_error('Page class does not implement method blocks_default_position()', E_USER_WARNING);
return NULL;
}
// If you don't override this, newly constructed pages of this kind won't have any blocks.
function blocks_get_default() {
return '';
}
// If you don't override this, your blocks will not be able to change positions
function blocks_move_position(&$instance, $move) {
return $instance->position;
}
// SELF-REPORTING SECTION
// Derived classes HAVE to define their "home url"
function url_get_path() {
trigger_error('Page class does not implement method url_get_path()', E_USER_WARNING);
return NULL;
}
// It's not always required to pass any arguments to the home url, so this doesn't trigger any errors (sensible default)
function url_get_parameters() {
return array();
}
// This should actually NEVER be overridden unless you have GOOD reason. Works fine as it is.
function url_get_full($extraparams = array()) {
$path = $this->url_get_path();
if(empty($path)) {
return NULL;
}
$params = $this->url_get_parameters();
if (!empty($params)) {
$params = array_merge($params, $extraparams);
} else {
$params = $extraparams;
}
if(empty($params)) {
return $path;
}
$first = true;
foreach($params as $var => $value) {
$path .= $first? '?' : '&';
$path .= $var .'='. urlencode($value);
$first = false;
}
return $path;
}
// This forces implementers to actually hardwire their page identification constant in the class.
// Good thing, if you ask me. That way we can later auto-detect "installed" page types by querying
// the classes themselves in the future.
function get_type() {
trigger_error('Page class does not implement method get_type()', E_USER_ERROR);
return NULL;
}
// Simple stuff, do not override this.
function get_id() {
return $this->id;
}
// "Sensible default" case here. Take it from the body id.
function get_format_name() {
return $this->body_id;
}
// Returns $this->body_class
function get_body_class() {
return $this->body_class;
}
// Returns $this->body_id
function get_body_id() {
return $this->body_id;
}
// Initialize the data members of the parent class
function init_quick($data) {
$this->type = $data->pagetype;
$this->id = $data->pageid;
}
function init_full() {
$this->full_init_done = true;
}
// is this page always editable, regardless of anything else?
function edit_always() {
return (has_capability('moodle/site:manageblocks', get_context_instance(CONTEXT_SYSTEM)) && defined('ADMIN_STICKYBLOCKS'));
}
}
/**
* Class that models the behavior of a moodle course
*
* @author Jon Papaioannou
* @package pages
*/
class page_course extends page_base {
// Any data we might need to store specifically about ourself should be declared here.
// After init_full() is called for the first time, ALL of these variables should be
// initialized correctly and ready for use.
var $courserecord = NULL;
// Do any validation of the officially recognized bits of the data and forward to parent.
// Do NOT load up "expensive" resouces (e.g. SQL data) here!
function init_quick($data) {
if(empty($data->pageid) && !defined('ADMIN_STICKYBLOCKS')) {
error('Cannot quickly initialize page: empty course id');
}
parent::init_quick($data);
}
// Here you should load up all heavy-duty data for your page. Basically everything that
// does not NEED to be loaded for the class to make basic decisions should NOT be loaded
// in init_quick() and instead deferred here. Of course this function had better recognize
// $this->full_init_done to prevent wasteful multiple-time data retrieval.
function init_full() {
if($this->full_init_done) {
return;
}
if (empty($this->id)) {
$this->id = 0; // avoid db errors
}
$this->courserecord = get_record('course', 'id', $this->id);
if(empty($this->courserecord) && !defined('ADMIN_STICKYBLOCKS')) {
error('Cannot fully initialize page: invalid course id '. $this->id);
}
$this->full_init_done = true;
}
// USER-RELATED THINGS
// Can user edit the course page or "sticky page"?
// This is also about editting of blocks BUT mainly activities in course page layout, see
// update_course_icon() - it must use the same capability
function user_allowed_editing() {
if (has_capability('moodle/site:manageblocks', get_context_instance(CONTEXT_SYSTEM)) && defined('ADMIN_STICKYBLOCKS')) {
return true;
}
return has_capability('moodle/course:manageactivities', get_context_instance(CONTEXT_COURSE, $this->id));
}
// Is the user actually editing this course page or "sticky page" right now?
function user_is_editing() {
if (has_capability('moodle/site:manageblocks', get_context_instance(CONTEXT_SYSTEM)) && defined('ADMIN_STICKYBLOCKS')) {
//always in edit mode on sticky page
return true;
}
return isediting($this->id);
}
// HTML OUTPUT SECTION
// This function prints out the common part of the page's header.
// You should NEVER print the header "by hand" in other code.
function print_header($title, $morebreadcrumbs=NULL, $meta='', $bodytags='',$parentlink=NULL) {
global $USER, $CFG;
$this->init_full();
$replacements = array(
'%fullname%' => $this->courserecord->fullname
);
foreach($replacements as $search => $replace) {
$title = str_replace($search, $replace, $title);
}
if($this->courserecord->id == SITEID) {
$breadcrumbs = array();
}
else {
if($parentlink) {
$breadcrumbs = array_merge($parentlink, array($this->courserecord->shortname => $CFG->wwwroot.'/course/view.php?id='.$this->courserecord->id));
} else {
$breadcrumbs = array($this->courserecord->shortname => $CFG->wwwroot.'/course/view.php?id='.$this->courserecord->id);
}
}
if(!empty($morebreadcrumbs)) {
$breadcrumbs = array_merge($breadcrumbs, $morebreadcrumbs);
}
$total = count($breadcrumbs);
$current = 1;
$crumbtext = '';
foreach($breadcrumbs as $text => $href) {
if($current++ == $total) {
$crumbtext .= ' '.$text;
}
else {
$crumbtext .= ' '.$text.' ->';
}
}
// The "Editing On" button will be appearing only in the "main" course screen
// (i.e., no breadcrumbs other than the default one added inside this function)
$buttons = switchroles_form($this->courserecord->id) . update_course_icon($this->courserecord->id );
$buttons = empty($morebreadcrumbs) ? $buttons : ' ';
print_header($title, $this->courserecord->fullname, $crumbtext,
'', $meta, true, $buttons, user_login_string($this->courserecord, $USER), false, $bodytags);
echo '';
}
// SELF-REPORTING SECTION
// This is hardwired here so the factory function page_create_object() can be sure there was no mistake.
// Also, it doubles as a way to let others inquire about our type.
function get_type() {
return PAGE_COURSE_VIEW;
}
// This is like the "category" of a page of this "type". For example, if the type is PAGE_COURSE_VIEW
// the format_name is the actual name of the course format. If the type were PAGE_ACTIVITY_VIEW, then
// the format_name might be that activity's name etc.
function get_format_name() {
$this->init_full();
if (defined('ADMIN_STICKYBLOCKS')) {
return PAGE_COURSE_VIEW;
}
if($this->id == SITEID) {
return parent::get_format_name();
}
// This needs to reflect the path hierarchy under Moodle root.
return 'course-view-'.$this->courserecord->format;
}
// This should return a fully qualified path to the URL which is responsible for displaying us.
function url_get_path() {
global $CFG;
if (defined('ADMIN_STICKYBLOCKS')) {
return $CFG->wwwroot.'/'.$CFG->admin.'/stickyblocks.php';
}
if($this->id == SITEID) {
return $CFG->wwwroot .'/index.php';
}
else {
return $CFG->wwwroot .'/course/view.php';
}
}
// This should return an associative array of any GET/POST parameters that are needed by the URL
// which displays us to make it work. If none are needed, return an empty array.
function url_get_parameters() {
if (defined('ADMIN_STICKYBLOCKS')) {
return array('pt' => ADMIN_STICKYBLOCKS);
}
if($this->id == SITEID) {
return array();
}
else {
return array('id' => $this->id);
}
}
// BLOCKS RELATED SECTION
// Which are the positions in this page which support blocks? Return an array containing their identifiers.
// BE CAREFUL, ORDER DOES MATTER! In textual representations, lists of blocks in a page use the ':' character
// to delimit different positions in the page. The part before the first ':' in such a representation will map
// directly to the first item of the array you return here, the second to the next one and so on. This way,
// you can add more positions in the future without interfering with legacy textual representations.
function blocks_get_positions() {
return array(BLOCK_POS_LEFT, BLOCK_POS_RIGHT);
}
// When a new block is created in this page, which position should it go to?
function blocks_default_position() {
return BLOCK_POS_RIGHT;
}
// When we are creating a new page, use the data at your disposal to provide a textual representation of the
// blocks that are going to get added to this new page. Delimit block names with commas (,) and use double
// colons (:) to delimit between block positions in the page. See blocks_get_positions() for additional info.
function blocks_get_default() {
global $CFG;
$this->init_full();
if($this->id == SITEID) {
// Is it the site?
if (!empty($CFG->defaultblocks_site)) {
$blocknames = $CFG->defaultblocks_site;
}
/// Failsafe - in case nothing was defined.
else {
$blocknames = 'site_main_menu,admin_tree:course_summary,calendar_month';
}
}
// It's a normal course, so do it according to the course format
else {
$pageformat = $this->courserecord->format;
if (!empty($CFG->{'defaultblocks_'. $pageformat})) {
$blocknames = $CFG->{'defaultblocks_'. $pageformat};
}
else {
$format_config = $CFG->dirroot.'/course/format/'.$pageformat.'/config.php';
if (@is_file($format_config) && is_readable($format_config)) {
require($format_config);
}
if (!empty($format['defaultblocks'])) {
$blocknames = $format['defaultblocks'];
}
else if (!empty($CFG->defaultblocks)){
$blocknames = $CFG->defaultblocks;
}
/// Failsafe - in case nothing was defined.
else {
$blocknames = 'participants,activity_modules,search_forums,admin,course_list:news_items,calendar_upcoming,recent_activity';
}
}
}
return $blocknames;
}
// Given an instance of a block in this page and the direction in which we want to move it, where is
// it going to go? Return the identifier of the instance's new position. This allows us to tell blocklib
// how we want the blocks to move around in this page in an arbitrarily complex way. If the move as given
// does not make sense, make sure to return the instance's original position.
//
// Since this is going to get called a LOT, pass the instance by reference purely for speed. Do **NOT**
// modify its data in any way, this will actually confuse blocklib!!!
function blocks_move_position(&$instance, $move) {
if($instance->position == BLOCK_POS_LEFT && $move == BLOCK_MOVE_RIGHT) {
return BLOCK_POS_RIGHT;
} else if ($instance->position == BLOCK_POS_RIGHT && $move == BLOCK_MOVE_LEFT) {
return BLOCK_POS_LEFT;
}
return $instance->position;
}
}
/**
* Class that models the common parts of all activity modules
*
* @author Jon Papaioannou
* @package pages
*/
class page_generic_activity extends page_base {
var $activityname = NULL;
var $courserecord = NULL;
var $modulerecord = NULL;
var $activityrecord = NULL;
function init_full() {
if($this->full_init_done) {
return;
}
if(empty($this->activityname)) {
error('Page object derived from page_generic_activity but did not define $this->activityname');
}
$module = get_record('modules', 'name', $this->activityname);
$this->modulerecord = get_record('course_modules', 'module', $module->id, 'instance', $this->id);
if(empty($this->modulerecord)) {
error('Cannot fully initialize page: invalid '.$this->activityname.' instance id '. $this->id);
}
$this->courserecord = get_record('course', 'id', $this->modulerecord->course);
if(empty($this->courserecord)) {
error('Cannot fully initialize page: invalid course id '. $this->modulerecord->course);
}
$this->activityrecord = get_record($this->activityname, 'id', $this->id);
if(empty($this->courserecord)) {
error('Cannot fully initialize page: invalid '.$this->activityname.' id '. $this->id);
}
$this->full_init_done = true;
}
function user_allowed_editing() {
$this->init_full();
return has_capability('moodle/site:manageblocks', get_context_instance(CONTEXT_COURSE, $this->modulerecord->course));
}
function user_is_editing() {
$this->init_full();
return isediting($this->modulerecord->course);
}
function url_get_path() {
global $CFG;
return $CFG->wwwroot .'/mod/'.$this->activityname.'/view.php';
}
function url_get_parameters() {
$this->init_full();
return array('id' => $this->modulerecord->id);
}
function blocks_get_positions() {
return array(BLOCK_POS_LEFT);
}
function blocks_default_position() {
return BLOCK_POS_LEFT;
}
}
?>